#include "qlevelview.h"
#include <QPainter>
#include <QBitmap>
#include <QApplication>
#include <QDir>
#include <QMouseEvent>
#include "mainwindow.h"
#include "ui_mainwindow.h"

QLevelView::QLevelView(QWidget *parent) :
    QWidget(parent)
{
    CurrentTextColour = Qt::white;
    SelectedTile = QPoint(0,0);
    setMouseTracking(true);
}

void QLevelView::DrawTile(int iTileNo, int iX, int iY)
{
    if ((iX >= iLevelX) || (iY >= iLevelY))
        return;

    if ((iTileNo < 0) || (iTileNo > 266))
        return;

    QPainter p(this);

    //Check for colour tile
    if ((iTileNo >= 0xBE) && (iTileNo <= 0xC7))
        CurrentTextColour = TileToColour(iTileNo);

    //Check for Char Tile
    if ((iTileNo >= 0xC8) && (iTileNo < 0xFF))
    {
        p.setBackgroundMode(Qt::OpaqueMode);
        p.setBackground(QBrush(CurrentTextColour));
        p.setPen(Qt::black);
        p.drawPixmap(QPoint(iX*iTileSize, iY*iTileSize), MainWin->Tiles[iTileNo]);
        return;
    }

    //display correctly coloured tiles
    // 0x03 0x04 0x05 - 0x0B 0x0C 0x0D - 0x0E 0x0F 0x10 - 0x11 0x12 0x13 - 0x33 0x34
    if (MainWin->ui->ckbColours->isChecked())
    {
        switch (iTileNo)
        {
            case 0x03: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[0]); return;
            case 0x04: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[1]); return;
            case 0x05: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[2]); return;
            case 0x0B: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[3]); return;
            case 0x0C: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[4]); return;
            case 0x0D: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[5]); return;
            case 0x0E: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[6]); return;
            case 0x0F: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[7]); return;
            case 0x10: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[8]); return;
            case 0x11: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[9]); return;
            case 0x12: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[10]); return;
            case 0x13: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[11]); return;
            case 0x33: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[12]); return;
            case 0x34: p.drawImage(QPoint(iX*iTileSize, iY*iTileSize), RecolouredTiles[13]); return;
        }
    }
    p.drawPixmap(QPoint(iX*iTileSize, iY*iTileSize), MainWin->Tiles[iTileNo]);
}

QColor QLevelView::TileToColour(unsigned char iTile)
{
    switch (iTile)
    {
        case 0xC2: return LevelBackgroundColour;
        case 0xBF: return QColor(85, 255, 85);
        case 0xBE: return QColor(85, 85, 255);
        case 0xC0: return QColor(255, 85, 85);
        case 0xC1: return QColor(255, 85, 255);

        default: return QColor(85, 255, 85);
     }
}

QColor QLevelView::HexToColour(int iHexCode)
{
    iHexCode = iHexCode % 0x20;
    switch (iHexCode)
    {
        case 0x00: case 0x08: return QColor(0, 0, 0);
        case 0x01: case 0x09: return QColor(0, 0, 170);
        case 0x02: case 0x0A: return QColor(0, 170, 0);
        case 0x03: case 0x0B: return QColor(0, 170, 170);
        case 0x04: case 0x0C: return QColor(170, 0, 0);
        case 0x05: case 0x0D: return QColor(170, 0, 170);
        case 0x06: case 0x0E: return QColor(170, 85, 0);
        case 0x07: case 0x0F: return QColor(170, 170, 170);
        case 0x10: case 0x18: return QColor(85, 85, 85);
        case 0x11: case 0x19: return QColor(85, 85, 255);
        case 0x12: case 0x1A: return QColor(85, 255, 85);
        case 0x13: case 0x1B: return QColor(85, 255, 255);
        case 0x14: case 0x1C: return QColor(255, 85, 85);
        case 0x15: case 0x1D: return QColor(255, 85, 255);
        case 0x16: case 0x1E: return QColor(255, 255, 85);
        case 0x17: case 0x1F: return QColor(255, 255, 255);

        default: return QColor(85, 255, 85);
    }
}

unsigned char QLevelView::ColourToHex(QColor Colour)
{
    if (Colour == QColor(0, 0, 0))
        return 0x00;
    if (Colour == QColor(0, 0, 170))
        return 0x01;
    if (Colour == QColor(0, 170, 0))
        return 0x02;
    if (Colour == QColor(0, 170, 170))
        return 0x03;
    if (Colour == QColor(170, 0, 0))
        return 0x04;
    if (Colour == QColor(170, 0, 170))
        return 0x05;
    if (Colour == QColor(170, 85, 0))
        return 0x06;
    if (Colour == QColor(170, 170, 170))
        return 0x07;
    if (Colour == QColor(85, 85, 85))
        return 0x10;
    if (Colour == QColor(85, 85, 255))
        return 0x11;
    if (Colour == QColor(85, 255, 85))
        return 0x12;
    if (Colour == QColor(85, 255, 255))
        return 0x13;
    if (Colour == QColor(255, 85, 85))
        return 0x14;
    if (Colour == QColor(255, 85, 255))
        return 0x15;
    if (Colour == QColor(255, 255, 85))
        return 0x16;
    if (Colour == QColor(255, 255, 255))
        return 0x17;

    return 0x00;
}

void QLevelView::paintEvent(QPaintEvent *)
{
    //draw tiles
    for (int i = 0; i < iLevelY; i++)
        for (int j = 0; j < iLevelX; j++)
            DrawTile(CurrentLevel[i*iLevelX+j], j, i);

    QPainter painter(this);

    //draw exit
    if (MainWin->ui->ckbExit->isChecked())
        painter.drawPixmap(QPoint(Door.iPosXPixel, Door.iPosYPixel), MainWin->Tiles[iDoorTile]);

    //draw boards
    for (int i = 0; i < iMaxBoards; i++)
    {
        if (!Boards[i].bExists)
            continue;
        if (MainWin->ui->ckbColours->isChecked())
            painter.drawImage(QPoint(Boards[i].iPosXPixel, Boards[i].iPosYPixel), RecolouredTiles[Boards[i].iTile-0x27]);
        else
            painter.drawPixmap(QPoint(Boards[i].iPosXPixel, Boards[i].iPosYPixel), MainWin->Tiles[Boards[i].iTile]);
        //draw board info
        if (MainWin->ui->ckbBoards->isChecked())
        {
            //draw distance
            painter.setPen(Qt::gray);
            switch (Boards[i].iType)
            {
                case 0x01: case 0x03: case 0x04:
                    if (Boards[i].iRev)
                        painter.drawRect(Boards[i].iPosXPixel+Boards[i].iWidthPixel-1, Boards[i].iPosYPixel-1, -1*(Boards[i].iDistPixel+1), 7);
                    else
                        painter.drawRect(Boards[i].iPosXPixel-1, Boards[i].iPosYPixel-1, Boards[i].iDistPixel+1, 7);
                    break;
                case 0x02: case 0x05:
                    if (Boards[i].iRev)
                        painter.drawRect(Boards[i].iPosXPixel-1, Boards[i].iPosYPixel+5, Boards[i].iWidthPixel+1, -1*(Boards[i].iDistPixel+1));
                    else
                        painter.drawRect(Boards[i].iPosXPixel-1, Boards[i].iPosYPixel-1, Boards[i].iWidthPixel+1, Boards[i].iDistPixel+1);
                    break;
                case 0x06:
                    if (Boards[i].iRev)
                        painter.drawRect(Boards[i].iPosXPixel, Boards[i].iPosYPixel, Boards[i].iWidthPixel, Boards[i].iDistPixel);
                    else
                        painter.drawRect(Boards[i].iPosXPixel-1, Boards[i].iPosYPixel+5, Boards[i].iWidthPixel+1, -1*(Boards[i].iDistPixel+1));
                    break;
            }
            //draw board number
            painter.setBackgroundMode(Qt::OpaqueMode);
            painter.setPen(Qt::white);
            painter.setBackground(QBrush(Qt::black));
            painter.drawText(QPoint(Boards[i].iPosXPixel, Boards[i].iPosYPixel - 5), QString("%1").arg(i+1));

            /* Use groupboxes now...
            //show description
            sDesc = "";
            //add stop type
            switch (Boards[i].iStop)
            {
                case 0x01: sDesc.append("STOPw "); break;
                case 0x02: sDesc.append("STOPwo "); break;
            }
            //add info for type 0x06
            if (Boards[i].iType == 0x06)
                sDesc.append("WaitTP ");
            //add info about delay
            if (Boards[i].iDelay)
                sDesc.append(QString("d%1").arg(Boards[i].iDelay, 2, 16, QChar('0')));

            painter.setPen(Qt::white);
            painter.setBackground(QBrush(Qt::black));
            painter.drawText(QPoint(Boards[i].iPosXPixel, Boards[i].iPosYPixel - 3), sDesc); */
        }
    }

    //draw selection
    painter.setPen(Qt::white);
    painter.drawRect(SelectedTile.x() * iTileSize, SelectedTile.y() * iTileSize, iTileSize, iTileSize);
}

void QLevelView::updateView()
{  
    //new level
    //update door struct
    Door.iPosX = CurrentLevel[iDoorOffset + 1] * 0x100 + CurrentLevel[iDoorOffset];
    Door.iPosY = CurrentLevel[iDoorOffset + 2];
    Door.iPosXPixel = 2*Door.iPosX;
    Door.iPosYPixel = 2*Door.iPosY - 18 - 42;

    //update boards
    for (int i = 0; i < iMaxBoards; i++)
    {
        Boards[i].iPosX = CurrentLevel[iBoardOffset + 1 + (iBoardSize * i)] * 0x100 + CurrentLevel[iBoardOffset + (iBoardSize * i)];
        Boards[i].iPosY = CurrentLevel[iBoardOffset + 3 + (iBoardSize * i)] * 0x100 + CurrentLevel[iBoardOffset + 2 + (iBoardSize * i)];
        Boards[i].iTile = CurrentLevel[iBoardOffset + 8 + (iBoardSize * i)];
        if (!Boards[i].iPosX || !Boards[i].iPosY)
        {
            Boards[i].bExists = false;
            continue;
        }
        Boards[i].bExists = true;
        Boards[i].iPosXPixel = 2*Boards[i].iPosX;
        Boards[i].iPosYPixel = 2*Boards[i].iPosY - 20;
        Boards[i].iWidth = CurrentLevel[iBoardOffset + 4 + (iBoardSize * i)];
        Boards[i].iWidthPixel = 2*Boards[i].iWidth;
        Boards[i].iDelay = CurrentLevel[iBoardOffset + 14 + (iBoardSize * i)];
        Boards[i].iType = CurrentLevel[iBoardOffset + 18 + (iBoardSize * i)];
        Boards[i].iStop = CurrentLevel[iBoardOffset + 22 + (iBoardSize * i)];
        Boards[i].iDist = CurrentLevel[iBoardOffset + 25 + (iBoardSize * i)] * 0x100 + CurrentLevel[iBoardOffset + 24 + (iBoardSize * i)];
        Boards[i].iDistPixel = 2*Boards[i].iDist;
        Boards[i].iRev = CurrentLevel[iBoardOffset + 30 + (iBoardSize * i)];

        switch (Boards[i].iType)
        {
            case 0x01: Boards[i].iDistPixel = 13 * iTileSize; break;
            case 0x02: Boards[i].iDistPixel = 14 * iTileSize + 6; break;
            case 0x03: Boards[i].iDistPixel = 9 * iTileSize; break;
            case 0x06: Boards[i].iDistPixel = Boards[i].iDistPixel*5 - 5; break;
        }

        //iBoardCount++;
    }

    //get level colours
    LevelForegroundColour = HexToColour(CurrentLevel[iLevelColourOffset]);
    LevelBackgroundColour = HexToColour(CurrentLevel[iLevelColourOffset+1]);

    //QString sDump = "";
    //for (int i = -1; i < 2; i++)
        //sDump.append(QString("%1 ").arg(CurrentLevel[iLevelColourOffset + i], 2, 16, QChar('0')));
    //qDebug("Level %d: Color Dump: %s", MainWin->ui->spinBox->text().toInt(0, 0), sDump.toAscii().constData());

    //create correctly coloured tiles
    // 0x03 0x04 0x05 - 0x0B 0x0C 0x0D - 0x0E 0x0F 0x10 - 0x11 0x12 0x13 - 0x33 0x34
    if (MainWin->ui->ckbColours->isChecked())
    {
        RecolouredTiles[0] = MainWin->Tiles[0x03].toImage();
        RecolouredTiles[1] = MainWin->Tiles[0x04].toImage();
        RecolouredTiles[2] = MainWin->Tiles[0x05].toImage();
        RecolouredTiles[3] = MainWin->Tiles[0x0B].toImage();
        RecolouredTiles[4] = MainWin->Tiles[0x0C].toImage();
        RecolouredTiles[5] = MainWin->Tiles[0x0D].toImage();
        RecolouredTiles[6] = MainWin->Tiles[0x0E].toImage();
        RecolouredTiles[7] = MainWin->Tiles[0x0F].toImage();
        RecolouredTiles[8] = MainWin->Tiles[0x10].toImage();
        RecolouredTiles[9] = MainWin->Tiles[0x11].toImage();
        RecolouredTiles[10] = MainWin->Tiles[0x12].toImage();
        RecolouredTiles[11] = MainWin->Tiles[0x13].toImage();
        RecolouredTiles[12] = MainWin->Tiles[0x33].toImage();
        RecolouredTiles[13] = MainWin->Tiles[0x34].toImage();

        for (int iImg = 0; iImg < 14; iImg++)
            for (int i = 0; i < RecolouredTiles[iImg].width(); i++)
                for(int j = 0; j < RecolouredTiles[iImg].height(); j++)
                    if (RecolouredTiles[iImg].pixel(i, j) == QColor(170, 0, 170).rgb())
                        RecolouredTiles[iImg].setPixel(i, j, LevelForegroundColour.rgb());
                    else if (RecolouredTiles[iImg].pixel(i, j) == QColor(85, 255, 85).rgb())
                        RecolouredTiles[iImg].setPixel(i, j, LevelBackgroundColour.rgb());
    }

    //bLevelChanged = false;

    MainWin->ui->grpBoard1->setEnabled(false);
    MainWin->ui->grpBoard2->setEnabled(false);
    MainWin->ui->grpBoard3->setEnabled(false);
    MainWin->ui->grpBoard4->setEnabled(false);
    MainWin->ui->grpBoard5->setEnabled(false);

    //Show board infos
    if (MainWin->ui->ckbBoards->isChecked())
        SetBoardBoxes();

    update();
}

void QLevelView::mousePressEvent(QMouseEvent *mouseevent)
{
    mouseMoveEvent(mouseevent);
}

void QLevelView::mouseMoveEvent(QMouseEvent *mouseevent)
{
    int iX = mouseevent->x();
    int iY = mouseevent->y();
    int iXTile = iX / iTileSize;
    int iYTile = iY / iTileSize;

    QPoint NewSelection = QPoint(iXTile, iYTile);
    if (SelectedTile != NewSelection)
    {
        SelectedTile = NewSelection;
        update();
    }
    if (mouseevent->buttons() == Qt::NoButton)
        return;

    //QListWidgetItem *Item = MainWin->ui->tileView->currentItem();
    //if (!Item && (mouseevent->buttons() == Qt::LeftButton))
        //return;
    QModelIndex mIndex = MainWin->ui->tileView->currentIndex();
    if (!mIndex.isValid() && (mouseevent->buttons() == Qt::LeftButton))
        return;

    int iTileNo;
    if (mouseevent->buttons() == Qt::LeftButton)
        //iTileNo = Item->text().toInt(0, 0);
        iTileNo = MainWin->iItemIndexToTile[mIndex.row()];
    else
        iTileNo = iEmptyTile;

    if (iTileNo == iDoorTile)
    {
        Door.iPosX = (iX - (iX % 2)) / 2;
        Door.iPosY = (iY - (iY % (iTileSize / 2) - (iTileSize / 2)) + 18) / 2;
        CurrentLevel[iDoorOffset + 2] = Door.iPosY;
        CurrentLevel[iDoorOffset + 1] = Door.iPosX / 0xFF;
        CurrentLevel[iDoorOffset] = Door.iPosX % 0xFF;
        Door.iPosXPixel = 2*Door.iPosX;
        Door.iPosYPixel = 2*Door.iPosY - 18 - 42;
        MainWin->ui->ckbExit->setChecked(true);
        if (!bLevelChanged)
            bLevelChanged = true;
        update();
        return;
    }

    int iSelectedBoard = 0;
    if (iTileNo == iBoard1Tile)
        iSelectedBoard = 1;
    else if (iTileNo == iBoard2Tile)
        iSelectedBoard = 2;
    else if (iTileNo == iBoard3Tile)
        iSelectedBoard = 3;
    else if (iTileNo == iBoard4Tile)
        iSelectedBoard = 4;
    else if (iTileNo == iBoard5Tile)
        iSelectedBoard = 5;
    if (iSelectedBoard)
    {
        if (!Boards[iSelectedBoard-1].bExists)
            CreateBoard(iSelectedBoard-1);
        Boards[iSelectedBoard-1].iPosX = iX / 2;
        Boards[iSelectedBoard-1].iPosXPixel = iX;
        Boards[iSelectedBoard-1].iPosY = (iY + 20) / 2;
        Boards[iSelectedBoard-1].iPosYPixel = iY;
        WriteBoard(iSelectedBoard-1);
        if (!MainWin->ui->ckbBoards->isChecked())
        {
            MainWin->ui->ckbBoards->setChecked(true);
            SetBoardBoxes();
        }
        if (!bLevelChanged)
            bLevelChanged = true;
        update();
        return;
    }

    if (CurrentLevel[iYTile*iLevelX+iXTile] != iTileNo)
    {
        CurrentLevel[iYTile*iLevelX+iXTile] = iTileNo;
        if (!bLevelChanged)
            bLevelChanged = true;
        update();
    }

}

void QLevelView::CreateBoard(int i)
{
    Boards[i].bExists = true;
    Boards[i].iDelay = 0x00;
    Boards[i].iRev = 0x00;
    Boards[i].iTile = 0x34;
    Boards[i].iType = 0x01;
    Boards[i].iWidth = 0x1B;
    Boards[i].iStop = 0x00;
    Boards[i].iDist = 0x00;
    Boards[i].iDistPixel = 13 * iTileSize;
    Boards[i].iWidthPixel = 2*Boards[i].iWidth;
    switch(i)
    {
        case 0: SetBoard1(Boards[i]); break;
        case 1: SetBoard2(Boards[i]); break;
        case 2: SetBoard3(Boards[i]); break;
        case 3: SetBoard4(Boards[i]); break;
        case 4: SetBoard5(Boards[i]); break;
    }
}

void QLevelView::WriteBoard(int i)
{
   CurrentLevel[iBoardOffset +  0 + (i * iBoardSize)] = Boards[i].iPosX % 0x100;
   CurrentLevel[iBoardOffset +  1 + (i * iBoardSize)] = Boards[i].iPosX / 0x100;
   CurrentLevel[iBoardOffset +  2 + (i * iBoardSize)] = Boards[i].iPosY % 0x100;
   CurrentLevel[iBoardOffset +  3 + (i * iBoardSize)] = Boards[i].iPosY / 0x100;
   CurrentLevel[iBoardOffset +  4 + (i * iBoardSize)] = Boards[i].iWidth;
   CurrentLevel[iBoardOffset +  5 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset +  6 + (i * iBoardSize)] = 0x03;
   CurrentLevel[iBoardOffset +  7 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset +  8 + (i * iBoardSize)] = Boards[i].iTile;
   CurrentLevel[iBoardOffset +  9 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 10 + (i * iBoardSize)] = 0x01;
   CurrentLevel[iBoardOffset + 11 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 12 + (i * iBoardSize)] = 0x01;
   CurrentLevel[iBoardOffset + 13 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 14 + (i * iBoardSize)] = Boards[i].iDelay;
   CurrentLevel[iBoardOffset + 15 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 16 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 17 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 18 + (i * iBoardSize)] = Boards[i].iType;
   CurrentLevel[iBoardOffset + 19 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 20 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 21 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 22 + (i * iBoardSize)] = Boards[i].iStop;
   CurrentLevel[iBoardOffset + 23 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 24 + (i * iBoardSize)] = Boards[i].iDist % 0x100;
   CurrentLevel[iBoardOffset + 25 + (i * iBoardSize)] = Boards[i].iDist / 0x100;
   CurrentLevel[iBoardOffset + 26 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 27 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 28 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 29 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 30 + (i * iBoardSize)] = Boards[i].iRev;
   CurrentLevel[iBoardOffset + 31 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 32 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 33 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 34 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 35 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 36 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 37 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 38 + (i * iBoardSize)] = 0x00;
   CurrentLevel[iBoardOffset + 39 + (i * iBoardSize)] = 0x00;
}

void QLevelView::SetBoardBoxes()
{
    for (int i = 0; i < iMaxBoards; i++)
        if (Boards[i].bExists)
            switch(i)
            {
                case 0: SetBoard1(Boards[i]); break;
                case 1: SetBoard2(Boards[i]); break;
                case 2: SetBoard3(Boards[i]); break;
                case 3: SetBoard4(Boards[i]); break;
                case 4: SetBoard5(Boards[i]); break;
            }

}

void QLevelView::SetBoard1(QRoboBoard B)
{
    MainWin->ui->grpBoard1->setEnabled(true);
    MainWin->ui->ckbB1Rev->setChecked(B.iRev == 0x24);
    MainWin->ui->spnB1Delay->setValue(B.iDelay);
    MainWin->ui->spnB1Dist->setValue(B.iDist);
    MainWin->ui->cmbB1Size->setCurrentIndex(B.iTile-0x33);
    MainWin->ui->cmbB1Type->setCurrentIndex(B.iType-1);
    MainWin->ui->cmbB1Stop->setCurrentIndex(B.iStop);
}

void QLevelView::SetBoard2(QRoboBoard B)
{
    MainWin->ui->grpBoard2->setEnabled(true);
    MainWin->ui->ckbB2Rev->setChecked(B.iRev == 0x24);
    MainWin->ui->spnB2Delay->setValue(B.iDelay);
    MainWin->ui->spnB2Dist->setValue(B.iDist);
    MainWin->ui->cmbB2Size->setCurrentIndex(B.iTile-0x33);
    MainWin->ui->cmbB2Type->setCurrentIndex(B.iType-1);
    MainWin->ui->cmbB2Stop->setCurrentIndex(B.iStop);
}

void QLevelView::SetBoard3(QRoboBoard B)
{
    MainWin->ui->grpBoard3->setEnabled(true);
    MainWin->ui->ckbB3Rev->setChecked(B.iRev == 0x24);
    MainWin->ui->spnB3Delay->setValue(B.iDelay);
    MainWin->ui->spnB3Dist->setValue(B.iDist);
    MainWin->ui->cmbB3Size->setCurrentIndex(B.iTile-0x33);
    MainWin->ui->cmbB3Type->setCurrentIndex(B.iType-1);
    MainWin->ui->cmbB3Stop->setCurrentIndex(B.iStop);
}

void QLevelView::SetBoard4(QRoboBoard B)
{
    MainWin->ui->grpBoard4->setEnabled(true);
    MainWin->ui->ckbB4Rev->setChecked(B.iRev == 0x24);
    MainWin->ui->spnB4Delay->setValue(B.iDelay);
    MainWin->ui->spnB4Dist->setValue(B.iDist);
    MainWin->ui->cmbB4Size->setCurrentIndex(B.iTile-0x33);
    MainWin->ui->cmbB4Type->setCurrentIndex(B.iType-1);
    MainWin->ui->cmbB4Stop->setCurrentIndex(B.iStop);
}

void QLevelView::SetBoard5(QRoboBoard B)
{
    MainWin->ui->grpBoard5->setEnabled(true);
    MainWin->ui->ckbB5Rev->setChecked(B.iRev == 0x24);
    MainWin->ui->spnB5Delay->setValue(B.iDelay);
    MainWin->ui->spnB5Dist->setValue(B.iDist);
    MainWin->ui->cmbB5Size->setCurrentIndex(B.iTile-0x33);
    MainWin->ui->cmbB5Type->setCurrentIndex(B.iType-1);
    MainWin->ui->cmbB5Stop->setCurrentIndex(B.iStop);
}

void QLevelView::deleteBoard(int i)
{
    Boards[i].bExists = false;
    switch(i)
    {
        case 0: MainWin->ui->grpBoard1->setEnabled(false); break;
        case 1: MainWin->ui->grpBoard2->setEnabled(false); break;
        case 2: MainWin->ui->grpBoard3->setEnabled(false); break;
        case 3: MainWin->ui->grpBoard4->setEnabled(false); break;
        case 4: MainWin->ui->grpBoard5->setEnabled(false); break;
    }
    for (int j = 0; j < iBoardSize; j++)
        CurrentLevel[iBoardOffset + j + (i * iBoardSize)] = 0x00;
    bLevelChanged = true;
    update();
}

void QLevelView::boardsChanged(int iBoard)
{
    WriteBoard(iBoard);
    update();
}
